package book

import grails.test.GrailsUnitTestCase;
import grails.test.mixin.*
import org.junit.*

/**
 * See the API for {@link grails.test.mixin.domain.DomainClassUnitTestMixin} for usage instructions
 */
@TestFor(Book)
class BookTests
{
	private Book getBook()
	{
		new Book(
		  author: "Anynomous",
		  category: BookCat.Science,
		  description:"This is a test book",
		  isbn: "ISBN 98-7654-321-0",
		  language: "english",
		  maxGrade: 2,
		  minGrade: 1,
		  title: "NoTitle"
		  )
	}
	
	@Test
    void testBook() 
	{
	  Book newBook = getBook()
	  newBook.save()	  
	  
	  println(newBook.id.toString())
	  
	  book.Book retrievedBook = Book.get(newBook.id)
	  
      assertEquals(newBook.author, retrievedBook.author)
	  assertEquals(newBook.category, retrievedBook.category)
	  assertEquals(newBook.description, retrievedBook.description)
	  assertEquals(newBook.isbn, retrievedBook.isbn)
	  assertEquals(newBook.language, retrievedBook.language)
	  assertEquals(newBook.minGrade, retrievedBook.minGrade)
	  assertEquals(newBook.maxGrade, retrievedBook.maxGrade)
	  assertEquals(newBook.title, retrievedBook.title)
    }
	
	@Test
    void testBookConstraint1() 
	{
		Book newBook = getBook()
		//ConstraintException
		newBook.author = ""
		assertFalse(newBook.validate())
	}
	
	@Test
    void testBookConstraint2() 
	{
		Book newBook = getBook()
		boolean thrown = false
		//ConstraintException
		newBook.isbn = "ISBN 10 Not An ISBN"
		assertFalse(newBook.validate())
	}
}
